package cn.bonoon.controllers;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.batatasframework.common.ParameterReader;
import org.batatasframework.immadiate.ImmadiatePlanDefinition;
import org.batatasframework.immadiate.ImmediateTaskInfo;
import org.batatasframework.web.RequestParameterReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bonoon.core.ImmediateTaskService;
import cn.bonoon.entities.ImmediateTaskEntity;
import cn.bonoon.kernel.support.IOperator;
import cn.bonoon.kernel.web.controllers.AbstractController;
import cn.bonoon.kernel.web.models.JsonResult;

@Controller
@RequestMapping("s/{catalog}!idt")
public class ImmediateTaskController extends AbstractController{
	
	@Autowired
	private ImmediateTaskService immediateTaskService;

	private String catalog;
	@ModelAttribute
	public void setCatalog(@PathVariable("catalog") String catalog) {
		this.catalog = catalog;
	}
	
	@RequestMapping("embed.htm")
	public String embed(Model model){
		model.addAttribute("layout", "layout-empty.vm");
		IOperator user = getUser();
		List<ImmediateTaskInfo> tasks = new ArrayList<>();
		ImmediateTaskInfo auto = immediateTaskService.myTask(user, catalog, tasks);
		model.addAttribute("mytasks", tasks);
		model.addAttribute("auto", auto);
		return "immadiate/embed";
	}

	@RequestMapping("load/{key}.htm")
	public String load(Model model, 
			HttpServletRequest request, 
			@PathVariable("key") String key){
		model.addAttribute("layout", "immadiate/layout-dialog.vm");
//		if(immadiateScheduleHandler != null){
			try{
				
				IOperator user = getUser();
				ImmadiatePlanDefinition def = immediateTaskService.findDefinition(catalog, key);
				
//				ImmediateTaskHandler<? extends ImmediateTaskEntity> taskHandler = immadiateScheduleHandler.findTaskHandler(key);
//				
				ParameterReader parameter = new RequestParameterReader(request);
				ImmediateTaskEntity task = immediateTaskService.loadToShow(def, user, parameter, model);
//				
				if(!parameter.getBoolean("autoopen", false)){
					immediateTaskService.accepted(user, task);
				}
				
				model.addAttribute("key", key);
				return def.getVmpath();
			}catch (Exception e) {
				log.error("加载任务[" + key + "]失败！", e);
			}
//		}
		return "immadiate/load-empty";
	}
	
	@ResponseBody
	@RequestMapping("material/delete/{key}!{taskid}!{id}.json")
	public JsonResult deleteMaterial(
			@PathVariable("key") String key, 
			@PathVariable("taskid") Long taskid, 
			@PathVariable("id") Long id){
		try{
			IOperator user = getUser();
			immediateTaskService.deleteMaterial(catalog, key, user, taskid, id);
			return JsonResult.result();
		}catch (Exception e) {
			return JsonResult.error(e);
		}
	}

	@ResponseBody
	@RequestMapping("task/update/{key}.json")
	public JsonResult update(HttpServletRequest request, 
			@PathVariable("key") String key){
		try{
			IOperator user = getUser();
			ParameterReader parameter = new RequestParameterReader(request);
			immediateTaskService.update(catalog, key, user, parameter);
			return JsonResult.result();
		}catch (Exception e) {
			return JsonResult.error(e);
		}
	}
	
	@RequestMapping("task/excel/{key}.excel")
	public void excel(HttpServletResponse response, HttpServletRequest request,
			@PathVariable("key") String key){
		IOperator user = getUser();
		ParameterReader parameter = new RequestParameterReader(request);
		immediateTaskService.export(user, catalog, key, parameter, response, request.getSession().getServletContext());
	}
}
